<template>
  <avue-form :option="option"
             v-model="form"
             @submit="submit">
  </avue-form>
</template>
<script>
import { mapGetters } from 'vuex'
import { findObj, saveObj } from '@/api/cust/health/health'
import option from '@/const/cust/health/jcInfo'
export default {
  data () {
    return {
      option: option,
      form: {
        jktjid: null,
        grjbxxid: null
      }
    }
  },
  watch: {
    'form.kqcl' () {
      this.colDisplay('kqcl', 'kqsfqc', '2', '缺齿信息')
      this.colDisplay('kqcl', 'kqsfquc', '3', '龋齿信息')
      this.colDisplay('kqcl', 'kqsfyc', '4', '义齿信息')
    },
    'form.chatiyandi' () {
      this.colDisplay('chatiyandi', 'chatiyandiDes', '2', '眼底异常说明')
    },
    'form.chatipf' () {
      this.colDisplay('chatipf', 'chatipfDes', '7', '皮肤其他说明')
    },
    'form.chatigm' () {
      this.colDisplay('chatigm', 'chatigmDes', '4', '巩膜其他说明')
    },
    'form.chatilbj' () {
      this.colDisplay('chatilbj', 'chatilbjDes', '4', '淋巴结其他说明')
    },
    'form.chatifeihxy' () {
      this.colDisplay('chatifeihxy', 'chatifeihxyDes', '2', '呼吸音异常说明')
    },
    'form.chatifeily' () {
      this.colDisplay('chatifeily', 'chatifeilyDes', '4', '罗音其他说明')
    },
    'form.chatixzxy' () {
      this.colDisplay('chatixzxy', 'chatixzxyDes', '2', '杂音说明')
    },
    'form.chatifbyt' () {
      this.colDisplay('chatifbyt', 'chatifbytDes', '2', '压痛说明')
    },
    'form.chatifbbk' () {
      this.colDisplay('chatifbbk', 'chatifbbkDes', '2', '包块说明')
    },
    'form.chatifbgd' () {
      this.colDisplay('chatifbgd', 'chatifbgdDes', '2', '肝大说明')
    },
    'form.chatifbpd' () {
      this.colDisplay('chatifbpd', 'chatifbpdDes', '2', '脾大说明')
    },
    'form.chatifbydxzy' () {
      this.colDisplay('chatifbydxzy', 'chatifbydxzyDes', '2', '移动性浊音说明')
    },
    'form.chatigmzz' () {
      this.colDisplay('chatigmzz', 'chatigmzzDes', '5', '肛门指诊其他说明')
    },
    'form.chatiruxian' () {
      this.colDisplay('chatiruxian', 'chatiruxianDes', '5', '乳腺其他说明')
    },
    'form.chatifkwy' () {
      this.colDisplay('chatifkwy', 'chatifkwyDes', '2', '外阴异常说明')
    },
    'form.chatifkyd' () {
      this.colDisplay('chatifkyd', 'chatifkydDes', '2', '阴道异常说明')
    },
    'form.chatifkgj' () {
      this.colDisplay('chatifkgj', 'chatifkgjDes', '2', '宫颈异常说明')
    },
    'form.chatifkgt' () {
      this.colDisplay('chatifkgt', 'chatifkgtDes', '2', '宫体异常说明')
    },
    'form.chatifkfj' () {
      this.colDisplay('chatifkfj', 'chatifkfjDes', '2', '附件异常说明')
    },
    'form.xdtzt' () {
      this.colDisplay('xdtzt', 'xdtztDes', '2', '心电图异常说明')
    },
    'form.xbxxp' () {
      this.colDisplay('xbxxp', 'xbxxpDes', '2', '胸部X线片异常说明')
    },
    'form.bchaofbbc' () {
      this.colDisplay('bchaofbbc', 'bchaofbbcDes', '2', '腹部B超异常说明')
    },
    'form.bchaoqtbbc' () {
      this.colDisplay('bchaoqtbbc', 'bchaoqtbbcDes', '2', '其他B超异常说明')
    },
    'form.gjgp' () {
      this.colDisplay('gjgp', 'gjgpDes', '2', '宫颈涂片异常说明')
    },

    'form.xczyjkwtnxgjb' () {
      this.colDisplay(
        'xczyjkwtnxgjb',
        'xczyjkwtnxgjbDes',
        '6',
        '脑血管疾病描述'
      )
    },
    'form.xczyjkwtszjb' () {
      this.colDisplay('xczyjkwtszjb', 'xczyjkwtszjbDes', '6', '肾脏疾病病描述')
    },
    'form.xczyjkwtxzjb' () {
      this.colDisplay('xczyjkwtxzjb', 'xczyjkwtxzjbDes', '7', '心脏疾病描述')
    },
    'form.xczyjkwtxgjb' () {
      this.colDisplay('xczyjkwtxgjb', 'xczyjkwtxgjbDes', '4', '血管疾病病描述')
    },
    'form.xczyjkwtybjb' () {
      this.colDisplay('xczyjkwtybjb', 'xczyjkwtybjbDes', '5', '眼部疾病描述')
    },
    'form.xczyjkwtsjxtjb' () {
      this.colDisplay(
        'xczyjkwtsjxtjb',
        'xczyjkwtsjxtjbDes',
        '2',
        '神经系统疾病描述'
      )
    },
    'form.xczyjkwtqtxtjb' () {
      this.colDisplay(
        'xczyjkwtqtxtjb',
        'xczyjkwtqtxtjbDes',
        '2',
        '其他系统疾病描述'
      )
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  created () {
    this.form.grjbxxid = this.$route.query.grjbxxid
    this.form.jktjid = this.$route.params.id
    //获取数据
    this.getTjb()
  },
  methods: {
    //保存数据
    submit () {
      saveObj(this.form).then(data => {
        this.from = data.data
        this.$message({
          showClose: true,
          message: '保存成功',
          type: 'success'
        })
      })
    },
    getTjb () {
      findObj(this.form).then(res => {
        if (res.data.code == 0 && res.data.data != null) {
          this.form = res.data.data[0]
        }
        if (this.validatenull(this.form.zqgnkqkc)) {
          this.form.zqgnkqkc = '1'
        }
        if (this.validatenull(this.form.kqcl)) {
          this.form.kqcl = '1'
        }

        if (this.validatenull(this.form.kqsfyc)) {
          this.form.kqsfyc = '1'
        }
        if (this.validatenull(this.form.kqyb)) {
          this.form.kqyb = '1'
        }
        if (this.validatenull(this.form.tingli)) {
          this.form.tingli = '1'
        }
        if (this.validatenull(this.form.ydgn)) {
          this.form.ydgn = '1'
        }
        if (this.validatenull(this.form.chatiyandi)) {
          this.form.chatiyandi = '1'
        }
        if (this.validatenull(this.form.chatipf)) {
          this.form.chatipf = '1'
        }
        if (this.validatenull(this.form.chatigm)) {
          this.form.chatigm = '1'
        }
        if (this.validatenull(this.form.chatilbj)) {
          this.form.chatilbj = '1'
        }
        if (this.validatenull(this.form.chatifeitzx)) {
          this.form.chatifeitzx = '1'
        }
        if (this.validatenull(this.form.chatifeihxy)) {
          this.form.chatifeihxy = '1'
        }
        if (this.validatenull(this.form.chatifeihxyDes)) {
          this.form.chatifeihxyDes = '1'
        }
        if (this.validatenull(this.form.chatifeily)) {
          this.form.chatifeily = '1'
        }
        if (this.validatenull(this.form.chatixzxlu)) {
          this.form.chatixzxlu = '1'
        }
        if (this.validatenull(this.form.chatixzxy)) {
          this.form.chatixzxy = '1'
        }
        if (this.validatenull(this.form.chatifbyt)) {
          this.form.chatifbyt = '1'
        }
        if (this.validatenull(this.form.chatifbytDes)) {
          this.form.chatifbytDes = '1'
        }
        if (this.validatenull(this.form.chatifbbk)) {
          this.form.chatifbbk = '1'
        }
        if (this.validatenull(this.form.chatifbgd)) {
          this.form.chatifbgd = '1'
        }
        if (this.validatenull(this.form.chatifbgdDes)) {
          this.form.chatifbgdDes = '1'
        }
        if (this.validatenull(this.form.chatifbpd)) {
          this.form.chatifbpd = '1'
        }
        if (this.validatenull(this.form.chatifbydxzy)) {
          this.form.chatifbydxzy = '1'
        }
        if (this.validatenull(this.form.chatixzsz)) {
          this.form.chatixzsz = '1'
        }
        if (this.validatenull(this.form.chatizbdmbd)) {
          this.form.chatizbdmbd = '2'
        }
        if (this.validatenull(this.form.xczyjkwtnxgjb)) {
          this.form.xczyjkwtnxgjb = '1'
        }
        if (this.validatenull(this.form.xczyjkwtszjb)) {
          this.form.xczyjkwtszjb = '1'
        }
        if (this.validatenull(this.form.xczyjkwtxzjb)) {
          this.form.xczyjkwtxzjb = '1'
        }
        if (this.validatenull(this.form.xczyjkwtxgjb)) {
          this.form.xczyjkwtxgjb = '1'
        }
        if (this.validatenull(this.form.xczyjkwtybjb)) {
          this.form.xczyjkwtybjb = '1'
        }
        if (this.validatenull(this.form.chatifkfj)) {
          this.form.chatifkfj = '1'
        }
        if (this.validatenull(this.form.xczyjkwtqtxtjb)) {
          this.form.xczyjkwtqtxtjb = '1'
        }
        if (this.validatenull(this.form.xczyjkwtsjxtjb)) {
          this.form.xczyjkwtsjxtjb = '1'
        }
      })
    },
    colDisplay (key1, key2, value, message) {
      var flag = false
      const des = this.findColumnIndex(key2)
      const desCol = this.option.group[des[0]].column[des[1]]
      const data = this.validatenull(this.form[key1])
        ? this.form[key1]
        : this.form[key1].split(',')
      flag = Array.isArray(data)
        ? data.includes(value)
        : this.form[key1] == value
      if (flag) {
        desCol.display = true
        desCol.rules = [
          {
            required: true,
            message: '请输入' + message
          }
        ]
      } else {
        desCol.display = false
        desCol.rules = []
      }
    },
    findColumnIndex (prop) {
      let formIndex = -1
      let groupIndex = 1
      this.option.group.forEach((ele, index) => {
        ele.column.forEach((cele, cindex) => {
          if (cele.prop === prop) {
            groupIndex = index
            formIndex = cindex
          }
        })
      })
      return [groupIndex, formIndex]
    }
  }
}
</script>